package com.cgy.utils.base.list;

import com.cgy.utils.base.arrays.ArraysHelper;

import java.util.*;

/**
 * list工具类
 * @author CHTK
 */
public class ListHelper {

    /**
     * 初始化
     *
     * @param <T>
     * @return
     */
    public static <T> List<T> newArrayList() {
        return new ArrayList<T>();
    }
    /**
     * 初始化
     *
     * @param <T>
     * @return
     */
    public static <T> List<T> newLinkedList() {
        return new LinkedList<>();
    }

    /**
     * 创建集合
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
	public static <T> List<T> unmodifiableList() {
        return (List<T>) Collections.unmodifiableCollection(newLinkedList());
    }

    /**
     * 获取有效结果
     * @param srcList
     * @param onlyHandler
     * @param <T>
     * @return
     */
    public static final <T>T only(List<T> srcList, OnlyHandler<T> onlyHandler) {
        if(isEmpty(srcList)) {
            return null;
        }
        if(srcList instanceof RandomAccess) {
            int size = srcList.size();
            T t;
            for (int i = 0; i < size; i++) {
                t = srcList.get(i);
                if(onlyHandler.only(t)) {
                    return t;
                }
            }
        } else {
            for (T t : srcList) {
                if(onlyHandler.only(t)) {
                    return t;
                }
            }
        }
        return null;
    }

    /**
     * 第一条数据
     * @param srcList
     * @return
     */
    public static <T>T one(List<T> srcList) {
        if(isEmpty(srcList)) {
            return null;
        }
        return srcList.get(0);
    }

    /**
     * 获取满足条件的结果集合
     * @param srcList
     * @param judgeHandler
     * @param <T>
     * @return
     */
    public static final <T>List<T> judge(List<T> srcList, JudgeHandler<T> judgeHandler) {
        List<T> newList = new ArrayList<>(srcList.size());
        if(isEmpty(srcList)) {
            return newList;
        }
        if(srcList instanceof RandomAccess) {
            int size = srcList.size();
            T t;
            for (int i = 0; i < size; i++) {
                t = srcList.get(i);
                if(judgeHandler.judge(t)) {
                    newList.add(t);
                }
            }
        } else {
            for (T t : srcList) {
                if(judgeHandler.judge(t)) {
                    newList.add(t);
                }
            }
        }
        return newList;
    }

    /**
     * 获取有效结果
     * @param srcList
     * @param <T>
     * @return
     */
    public static final <T>T only(List<T> srcList) {
        return only(srcList, new OnlyHandler<T>() {
            @Override
            public boolean only(T item) {
                return null != item;
            }
        });
    }


    /**
     * 初始化
     *
     * @param size 长度
     * @param <T>
     * @return
     */
    public static <T> List<T> newArrayList(final int size) {
        return new ArrayList<T>(size);
    }

    /**
     * 初始化
     *
     * @param t   数据
     * @param <T>
     * @return
     */
    @SafeVarargs
	public static <T> List<T> newArrayList(final T... t) {
        return ArraysHelper.newArrays(t);
    }

    /**
     * 遍历
     * @param <T>
     * @param sources 元数据
     * @param iteratorHandler 遍历方法
     * @return
     */
    public static <T> List<T> iterator(final List<T> sources, final IteratorHandler<T> iteratorHandler) {
        if(isEmpty(sources)) {
            return sources;
        }

        List<T> tList = newArrayList(listSize(sources));
        if(sources instanceof RandomAccess) {
            T t = null;
            for (int i = 0, size = sources.size(); i < size; i++) {
                t = sources.get(i);
                if(null != t  && null != iteratorHandler) {
                    t = iteratorHandler.next(t);
                    if(null != t) {
                        tList.add(t);
                    }
                }
            }
        } else {
            for (T t1 : sources) {
                if(null != iteratorHandler) {
                    t1 = iteratorHandler.next(t1);
                    if(null != t1) {
                        tList.add(t1);
                    }
                }
            }
        }
        return tList;
    }
    /**
     * 遍历
     * @param <T>
     * @param sources 元数据
     * @param iteratorHandler 遍历方法
     * @return
     */
    public static <T> List<T> iterator(final List<T> sources, final IteratorHandlerCount<T> iteratorHandler) {
        if(isEmpty(sources)) {
            return sources;
        }

        List<T> tList = newArrayList(listSize(sources));
        if(sources instanceof RandomAccess) {
            T t = null;
            for (int i = 0, size = sources.size(); i < size; i++) {
                t = sources.get(i);
                if(null != t  && null != iteratorHandler) {
                    t = iteratorHandler.next(t, i);
                    if(null != t) {
                        tList.add(t);
                    }
                }
            }
        } else {
            int count = 0;
            for (T t1 : sources) {
                if(null != iteratorHandler) {
                    t1 = iteratorHandler.next(t1, count ++);
                    if(null != t1) {
                        tList.add(t1);
                    }
                }
            }
        }
        return tList;
    }

    /**
     * 遍历
     * @param <T>
     * @param sources 元数据
     * @param iteratorHandler 遍历方法
     * @return
     */
    public static <T> void iterator(final List<T> sources, final IteratorHandlerEmpty<T> iteratorHandler) {
        if(isEmpty(sources)) {
            return;
        }

        if(sources instanceof RandomAccess) {
            T t = null;
            for (int i = 0, size = sources.size(); i < size; i++) {
                t = sources.get(i);
                if(null != t && null != iteratorHandler) {
                    iteratorHandler.next(t);
                }
            }
        } else {
            for (T t1 : sources) {
                if(null != iteratorHandler) {
                    iteratorHandler.next(t1);
                }
            }
        }
    }

    /**
     * 遍历
     * @param <T>
     * @param sources 元数据
     * @param iteratorHandler 遍历方法
     * @return
     */
    public static <T> void iterator(final List<T> sources, final IteratorHandlerCountEmpty<T> iteratorHandler) {
        if(isEmpty(sources)) {
            return;
        }

        if(sources instanceof RandomAccess) {
            T t = null;
            for (int i = 0, size = sources.size(); i < size; i++) {
                t = sources.get(i);
                if(null != t && null != iteratorHandler) {
                    iteratorHandler.next(t, i);
                }
            }
        } else {
            int count = 0;
            for (T t1 : sources) {
                if(null != iteratorHandler) {
                    iteratorHandler.next(t1, count ++);
                }
            }
        }
    }

    /**
     * 获取长度
     * @param sources
     * @param <T>
     * @return
     */
    public static <T> int listSize(List<T> sources) {
        return null == sources ? 0 : sources.size();
    }

    /**
     * 是否为空数据
     * @param sources 元数据
     * @param <T>
     * @return
     */
    public static <T>boolean isEmpty(final List<T> sources) {
        return listSize(sources) == 0;
    }

    /**
     * 添加
     * @param t 集合
     * @param t1 数据
     * @param <T>
     */
    public static <T> void add(List<T> t, final T t1) {
        if(isEmpty(t)) {
            t = newArrayList();
        }
        if(null != t1) {
            t.add(t1);
        }
    }

    /**
     * set 转 list
     * @param keySet
     * @param <T>
     * @return
     */
    public static <T> List<T> toList(Collection<T> keySet) {
        if(null != keySet) {
            List<T> kList = newArrayList(keySet.size());
            kList.addAll(keySet);
            return kList;
        }
        return newArrayList();
    }

    /**
     * 遍历
     */
    public static interface IteratorHandler<T> {
        /**
         * 每个节点
         * @param t 对象
         * @return
         */
        public T next(T t);
    }
    /**
     * 遍历
     */
    public static interface IteratorHandlerCount<T> {
        /**
         * 每个节点
         * @param index 索引
         * @param t 对象
         * @return
         */
        public T next(T t, final int index);
    }
    /**
     * 遍历
     */
    public static interface IteratorHandlerEmpty<T> {
        /**
         * 每个节点
         * @param t 对象
         * @return
         */
        public void next(T t);
    }
    /**
     * 遍历
     */
    public static interface IteratorHandlerCountEmpty<T> {
        /**
         * 每个节点
         * @param t 对象
         * @param index 索引
         * @return
         */
        public void next(T t, final int index);
    }


    /**
     * 唯一值
     * @see
     * @author Administrator
     */
    public static interface OnlyHandler<T> {
        /**
         *
         * @param item
         * @return
         */
        public boolean only(T item);
    }

    /**
     * 判断是否保留
     * @author CH
     */
    public static interface JudgeHandler<T> {
        /**
         *
         * @param item
         * @return
         */
        public boolean judge(T item);
    }
}
